const app = getApp();
import HttpRequst from '../../utils/httpRequst.js'
import API from '../../utils/config.js'
import WxValidate from '../../utils/WxValidate'
Page({

  data: {
    intervalTime: 60,//验证码倒计时的时间
    isInterval: false,//是否在验证码获取期间
    mobile: "",
   
    mobileHidden:"",
    Timer: "",
    isMobileValid:"",
    validateTipIsShow: false,
    validateTipMsg: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._initValidate();
    this.setData({
      mobile:options.mobile,
     
      isMobileValid: options.mobile,
      mobileHidden: options.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
    });
    wx.showModal({
      title: '提示',
      content: '您尚未绑定提现的支付宝账号',
      confirmText:"我知道了",
      showCancel:false
    })
    
  },
  formSubmit: function (e) {
    const params = e.detail.value;
    var that = this;
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0];
      this.setData({
        validateTipMsg: error.msg,
        validateTipIsShow: true
      })
      this._validateTipHide();
    } else {
      //HttpRequst(loading,url,发送header类型, sessionId, params,method, ask, function (res){})
      HttpRequst(true, API.bindUser, 1, app.getUkey(), params, "POST", false, function (res) {
          wx.navigateTo({
            url: '/pages/tiXian/tiXian'
          })
      })
    }

  },
  getCode: function (e) {
    var that = this;

    wx.request({
      url: API.baseUrl + API.getBindCode, 
      method: "POST",
      data: {
        mobile: this.data.mobile
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.code == 400) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });
          that.setData({
            isInterval: false
          })
        } else {
          that.setData({
            isInterval: true
          })
          that._countdownYzm();
        }

      }
    })





  },
  /**
    * 校验手机，决定是否可以获取手机短信验证码
    */
  // mobileInput: function (e) {
  //   this.setData({
  //     mobile: e.detail.value,
  //     isMobileValid: /^1[3456789]\d{9}$/.test(e.detail.value)
  //   })
  // },
  /**
   * 60秒后从新获取
   */
  _countdownYzm: function () {
    var that = this;
    var Timer = setInterval(function () {
      if (that.data.intervalTime <= 1) {
        clearInterval(Timer);
        that.setData({
          isInterval: false,
          intervalTime: 60
        });
      } else {
        that.setData({
          intervalTime: that.data.intervalTime - 1
        });
      }
    }, 1000)
  },
  /**
   * 2秒后隐藏错误提示信息
   */
  _validateTipHide: function () {
    var that = this;
    setTimeout(function () {
      that.setData({
        validateTipIsShow: false
      })
    }, 2000);
  },
  /**
   * 初始化验证插件
   */
  _initValidate: function () {
    const rules = {
      mobile: {
        required: true
       
      },
      code: {
        required: true
      },
      account: {
        required: true
      }
    };
    const messages = {
      mobile: {
        required: "请输入您的手机号码"
       
      },
      code: {
        required: "请输入短信验证码"
      },
      account:{
        required: "请输入支付宝账号"
      }
    };
    this.WxValidate = new WxValidate(rules, messages);
  }
})